package com.bjwl.service.safty.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bjwl.dao.safty.HomeDao;
import com.bjwl.model.Module;
import com.bjwl.service.safty.HomeService;
import com.bjwl.view.MenuView;
@Service
public class HomeServiceImpl implements HomeService {
	//声明Dao层类对象
	@Autowired
	private HomeDao homeDao; 
	@Override
	public List<MenuView> getMenuList(String u_id) {
		List<Module> moduleList=homeDao.queryModule(u_id);
		//所有菜单
		List<MenuView> menuList=new ArrayList<MenuView>();
		
		//主菜单
		
		MenuView mainMenu=null;
		for(Module module:moduleList) {
			//当主菜单为空或者主菜单的id不等于当前module的父id
			if(mainMenu==null||!mainMenu.getM_id().equals(module.getP_id())) {
				System.out.println(module.getM_id());
				//封装主菜单
				mainMenu=new MenuView();
				mainMenu.setM_id(module.getP_id());
				mainMenu.setM_name(module.getP_name());
				mainMenu.setSubMenuViews(new ArrayList<MenuView>());
				//将此主菜单添加到所有菜单列表中
				menuList.add(mainMenu);
				
			}
			
			//封装子菜单
			MenuView subMenu=new MenuView();
			subMenu.setM_id(module.getM_id());
			subMenu.setM_name(module.getM_name());
			subMenu.setM_url(module.getM_url());
			//将子菜单添加到主菜单中
			mainMenu.getSubMenuViews().add(subMenu);
		}
		return menuList;
	}

}
